package com.project.myPackage.common;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果，服务端响应的数据最终都会封装成此对象
 * @param <T>
 */
@Data
public class R<T> {

    private Integer status; //编码：0成功，1和其它数字为失败

    private String message; //错误信息

    private T data; //数据
    //是否关注
    @JsonProperty("isFavorite")//映射为json串中的属性名，处理映射错误情况会映射成followed
    private  boolean isFavorite;
    private Map map = new HashMap(); //动态数据
    private Integer token;

    public static <T> R<T> error(String message) {
        R r = new R();
        r.message = message;
        r.status = 1;
        return r;
    }
    public static <T> R<T> success(String message) {
        R<T> r = new R<T>();
        r.message = message;
        r.status = 0;
        return r;
    }
    public static <T> R<T> success(T object) {
        R<T> r = new R<T>();
        r.data = object;
        r.status = 0;
        return r;
    }
    //返回token
//    public static <T> R<T> success(T object,Integer token) {
//        R<T> r = new R<T>();
//        r.data = object;
//        r.token = token;
//        r.status = 0;
//        return r;
//    }
    public static <T> R<T> success(T object,String message) {
        R<T> r = new R<T>();
        r.data = object;
        r.message= message;
        r.status = 0;
        return r;
    }
    // 返回 token
    public static R<Map<String, Object>> success(String message,Integer token) {
        R<Map<String, Object>> r = new R<>();
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        r.message = message;
        r.token = token;
        r.data = data;
        r.status = 0;
        return r;
    }


 //是否关注
    public static <T> R<T> isFavorite(String message, Boolean isFavorite, T object) {
        R<T> r = new R<T>();
        r.data = object;
        r.message= message;
        r.isFavorite = isFavorite;
        r.status = 0;
        return r;
    }
    public static <T> R<T> isFavorite(String message, Boolean isFavorite) {
        R<T> r = new R<T>();
        r.message= message;
        r.isFavorite = isFavorite;
        r.status = 0;
        return r;
    }

    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
